home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / djgpp / src / gdb-4.12 / opcodes / sparc-di.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-03  |  14.0 KB  |  548 lines

  1. /* Print SPARC instructions.
  2.    Copyright 1989, 1991, 1992, 1993 Free Software Foundation, Inc.
  3.  
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8.  
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. GNU General Public License for more details.
  13.  
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. #include "ansidecl.h"
  19. #include "opcode/sparc.h"
  20. #include "dis-asm.h"
  21. #include <string.h>
  22.  
  23. /* Sign-extend a value which is N bits long.  */
  24. #define    SEX(value, bits) \
  25.     ((((int)(value)) << ((8 * sizeof (int)) - bits))    \
  26.              >> ((8 * sizeof (int)) - bits) )
  27.  
  28. static  char *reg_names[] =
  29. { "g0", "g1", "g2", "g3", "g4", "g5", "g6", "g7",    
  30.   "o0", "o1", "o2", "o3", "o4", "o5", "sp", "o7",    
  31.   "l0", "l1", "l2", "l3", "l4", "l5", "l6", "l7",    
  32.   "i0", "i1", "i2", "i3", "i4", "i5", "fp", "i7",    
  33.   "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",    
  34.   "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",    
  35.   "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
  36.   "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",
  37.   "y", "psr", "wim", "tbr", "pc", "npc", "fpsr", "cpsr"
  38. };
  39.  
  40. #define    freg_names    (®_names[4 * 8])
  41.  
  42.  
  43. /* FIXME--need to deal with byte order (probably using masking and
  44.    shifting rather than bitfields is easiest).  */
  45.  
  46. union sparc_insn
  47.   {
  48.     unsigned long int code;
  49.     struct
  50.       {
  51.     unsigned int anop:2;
  52. #define    op    ldst.anop
  53.     unsigned int anrd:5;
  54. #define    rd    ldst.anrd
  55.     unsigned int op3:6;
  56.     unsigned int anrs1:5;
  57. #define    rs1    ldst.anrs1
  58.     unsigned int i:1;
  59.     unsigned int anasi:8;
  60. #define    asi    ldst.anasi
  61.     unsigned int anrs2:5;
  62. #define    rs2    ldst.anrs2
  63. #define    shcnt    rs2
  64.       } ldst;
  65.     struct
  66.       {
  67.     unsigned int anop:2, anrd:5, op3:6, anrs1:5, i:1;
  68.     unsigned int IMM13:13;
  69. #define    imm13    IMM13.IMM13
  70.       } IMM13;
  71.     struct
  72.       {
  73.     unsigned int anop:2;
  74.     unsigned int a:1;
  75.     unsigned int cond:4;
  76.     unsigned int op2:3;
  77.     unsigned int DISP22:22;
  78. #define    disp22    branch.DISP22
  79. #define    imm22    disp22
  80.       } branch;
  81.     struct
  82.       {
  83.     unsigned int anop:2;
  84.     unsigned int adisp30:30;
  85. #define    disp30    call.adisp30
  86.       } call;
  87.   };
  88.  
  89. /* Nonzero if INSN is the opcode for a delayed branch.  */
  90. static int
  91. is_delayed_branch (insn)
  92.      union sparc_insn insn;
  93. {
  94.   unsigned int i;
  95.  
  96.   for (i = 0; i < NUMOPCODES; ++i)
  97.     {
  98.       CONST struct sparc_opcode *opcode = &sparc_opcodes[i];
  99.       if ((opcode->match & insn.code) == opcode->match
  100.       && (opcode->lose & insn.code) == 0)
  101.     return (opcode->flags & F_DELAYED);
  102.     }
  103.   return 0;
  104. }
  105.  
  106. static int opcodes_sorted = 0;
  107. /* extern void qsort (); */
  108. static int compare_opcodes ();
  109.  
  110. /* Print one instruction from MEMADDR on INFO->STREAM.
  111.  
  112.    We suffix the instruction with a comment that gives the absolute
  113.    address involved, as well as its symbolic form, if the instruction
  114.    is preceded by a findable `sethi' and it either adds an immediate
  115.    displacement to that register, or it is an `add' or `or' instruction
  116.    on that register.  */
  117. int
  118. print_insn_sparc (memaddr, info)
  119.      bfd_vma memaddr;
  120.      disassemble_info *info;
  121. {
  122.   FILE *stream = info->stream;
  123.   union sparc_insn insn;
  124.  
  125.   register unsigned int i;
  126.  
  127.   if (!opcodes_sorted)
  128.     {
  129.       qsort ((char *) sparc_opcodes, NUMOPCODES,
  130.          sizeof (sparc_opcodes[0]), compare_opcodes);
  131.       opcodes_sorted = 1;
  132.     }
  133.  
  134.   {
  135.     int status =
  136.       (*info->read_memory_func) (memaddr, (bfd_byte *) &insn, sizeof (insn), info);
  137.     if (status != 0)
  138.       {
  139.     (*info->memory_error_func) (status, memaddr, info);
  140.     return -1;
  141.       }
  142.   }
  143.  
  144.   info->insn_info_valid = 1;            /* We do return this info */
  145.   info->insn_type = dis_nonbranch;        /* Assume non branch insn */
  146.   info->branch_delay_insns = 0;            /* Assume no delay */
  147.   info->target = 0;                /* Assume no target known */
  148.  
  149.   for (i = 0; i < NUMOPCODES; ++i)
  150.     {
  151.       CONST struct sparc_opcode *opcode = &sparc_opcodes[i];
  152.       if ((opcode->match & insn.code) == opcode->match
  153.       && (opcode->lose & insn.code) == 0)
  154.     {
  155.       /* Nonzero means that we have found an instruction which has
  156.          the effect of adding or or'ing the imm13 field to rs1.  */
  157.       int imm_added_to_rs1 = 0;
  158.  
  159.       /* Nonzero means that we have found a plus sign in the args
  160.          field of the opcode table.  */
  161.       int found_plus = 0;
  162.       
  163.       /* Nonzero means we have an annulled branch.  */
  164.       int is_annulled = 0;
  165.  
  166.       /* Do we have an `add' or `or' instruction where rs1 is the same
  167.          as rsd, and which has the i bit set?  */
  168.       if ((opcode->match == 0x80102000 || opcode->match == 0x80002000)
  169.       /*              (or)                 (add)  */
  170.           && insn.rs1 == insn.rd)
  171.         imm_added_to_rs1 = 1;
  172.  
  173.       if (insn.rs1 != insn.rd
  174.           && strchr (opcode->args, 'r') != 0)
  175.           /* Can't do simple format if source and dest are different.  */
  176.           continue;
  177.  
  178.       (*info->fprintf_func) (stream, opcode->name);
  179.  
  180.       {
  181.         register CONST char *s;
  182.  
  183.         if (opcode->args[0] != ',')
  184.           (*info->fprintf_func) (stream, " ");
  185.         for (s = opcode->args; *s != '\0'; ++s)
  186.           {
  187.         while (*s == ',')
  188.           {
  189.             (*info->fprintf_func) (stream, ",");
  190.             ++s;
  191.             switch (*s) {
  192.             case 'a':
  193.               (*info->fprintf_func) (stream, "a");
  194.               is_annulled = 1;
  195.               ++s;
  196.               continue;
  197.  
  198.             default:
  199.               break;
  200.             }        /* switch on arg */
  201.           }        /* while there are comma started args */
  202.  
  203.         (*info->fprintf_func) (stream, " ");
  204.             
  205.         switch (*s)
  206.           {
  207.           case '+':
  208.             found_plus = 1;
  209.  
  210.             /* note fall-through */
  211.           default:
  212.             (*info->fprintf_func) (stream, "%c", *s);
  213.             break;
  214.  
  215.           case '#':
  216.             (*info->fprintf_func) (stream, "0");
  217.             break;
  218.  
  219. #define    reg(n)    (*info->fprintf_func) (stream, "%%%s", reg_names[n])
  220.           case '1':
  221.           case 'r':
  222.             reg (insn.rs1);
  223.             break;
  224.  
  225.           case '2':
  226.             reg (insn.rs2);
  227.             break;
  228.  
  229.           case 'd':
  230.             reg (insn.rd);
  231.             break;
  232. #undef    reg
  233.  
  234. #define    freg(n)        (*info->fprintf_func) (stream, "%%%s", freg_names[n])
  235. #define    fregx(n)    (*info->fprintf_func) (stream, "%%%s", freg_names[((n) & ~1) | (((n) & 1) << 5)])
  236.           case 'e':
  237.             freg (insn.rs1);
  238.             break;
  239.           case 'v':    /* double/even */
  240.           case 'V':    /* quad/multiple of 4 */
  241.             fregx (insn.rs1);
  242.             break;
  243.  
  244.           case 'f':
  245.             freg (insn.rs2);
  246.             break;
  247.           case 'B':    /* double/even */
  248.           case 'R':    /* quad/multiple of 4 */
  249.             fregx (insn.rs2);
  250.             break;
  251.  
  252.           case 'g':
  253.             freg (insn.rd);
  254.             break;
  255.           case 'H':    /* double/even */
  256.           case 'J':    /* quad/multiple of 4 */
  257.             fregx (insn.rd);
  258.             break;
  259. #undef    freg
  260. #undef    fregx
  261.  
  262. #define    creg(n)    (*info->fprintf_func) (stream, "%%c%u", (unsigned int) (n))
  263.           case 'b':
  264.             creg (insn.rs1);
  265.             break;
  266.  
  267.           case 'c':
  268.             creg (insn.rs2);
  269.             break;
  270.  
  271.           case 'D':
  272.             creg (insn.rd);
  273.             break;
  274. #undef    creg
  275.  
  276.           case 'h':
  277.             (*info->fprintf_func) (stream, "%%hi(%#x)",
  278.                        0xFFFFFFFF & (int) insn.imm22 << 10);
  279.             break;
  280.  
  281.           case 'i':
  282.             {
  283.               /* We cannot trust the compiler to sign-extend
  284.              when extracting the bitfield, hence the shifts.  */
  285.               int imm = SEX (insn.imm13, 13);
  286.  
  287.               /* Check to see whether we have a 1+i, and take
  288.              note of that fact.
  289.  
  290.              Note: because of the way we sort the table,
  291.              we will be matching 1+i rather than i+1,
  292.              so it is OK to assume that i is after +,
  293.              not before it.  */
  294.               if (found_plus)
  295.             imm_added_to_rs1 = 1;
  296.               
  297.               if (imm <= 9)
  298.             (*info->fprintf_func) (stream, "%d", imm);
  299.               else
  300.             (*info->fprintf_func) (stream, "%#x", imm);
  301.             }
  302.             break;
  303.  
  304.  
  305.           case 'M':
  306.             (*info->fprintf_func) (stream, "%%asr%d", insn.rs1);
  307.             break;
  308.             
  309.           case 'm':
  310.             (*info->fprintf_func) (stream, "%%asr%d", insn.rd);
  311.             break;
  312.             
  313.           case 'L':
  314.             info->target = memaddr + insn.disp30 * 4;
  315.             (*info->print_address_func) (info->target, info);
  316.             break;
  317.  
  318.           case 'n':
  319.             (*info->fprintf_func)
  320.               (stream, "%#x", (SEX (insn.disp22, 22)));
  321.             break;
  322.  
  323.           case 'l':
  324.             info->target = memaddr + (SEX (insn.disp22, 22)) * 4;
  325.             (*info->print_address_func) (info->target, info);
  326.             break;
  327.  
  328.           case 'A':
  329.             (*info->fprintf_func) (stream, "(%d)", (int) insn.asi);
  330.             break;
  331.  
  332.           case 'C':
  333.             (*info->fprintf_func) (stream, "%%csr");
  334.             break;
  335.  
  336.           case 'F':
  337.             (*info->fprintf_func) (stream, "%%fsr");
  338.             break;
  339.  
  340.           case 'p':
  341.             (*info->fprintf_func) (stream, "%%psr");
  342.             break;
  343.  
  344.           case 'q':
  345.             (*info->fprintf_func) (stream, "%%fq");
  346.             break;
  347.  
  348.           case 'Q':
  349.             (*info->fprintf_func) (stream, "%%cq");
  350.             break;
  351.  
  352.           case 't':
  353.             (*info->fprintf_func) (stream, "%%tbr");
  354.             break;
  355.  
  356.           case 'w':
  357.             (*info->fprintf_func) (stream, "%%wim");
  358.             break;
  359.  
  360.           case 'y':
  361.             (*info->fprintf_func) (stream, "%%y");
  362.             break;
  363.           }
  364.           }
  365.       }
  366.  
  367.       /* If we are adding or or'ing something to rs1, then
  368.          check to see whether the previous instruction was
  369.          a sethi to the same register as in the sethi.
  370.          If so, attempt to print the result of the add or
  371.          or (in this context add and or do the same thing)
  372.          and its symbolic value.  */
  373.       if (imm_added_to_rs1)
  374.         {
  375.           union sparc_insn prev_insn;
  376.           int errcode;
  377.  
  378.           errcode =
  379.         (*info->read_memory_func)
  380.           (memaddr - 4,
  381.            (bfd_byte *)&prev_insn, sizeof (prev_insn), info);
  382.  
  383.           if (errcode == 0)
  384.         {
  385.           /* If it is a delayed branch, we need to look at the
  386.              instruction before the delayed branch.  This handles
  387.              sequences such as
  388.  
  389.              sethi %o1, %hi(_foo), %o1
  390.              call _printf
  391.              or %o1, %lo(_foo), %o1
  392.              */
  393.  
  394.           if (is_delayed_branch (prev_insn))
  395.             errcode = (*info->read_memory_func)
  396.               (memaddr - 8, (bfd_byte *)&prev_insn, sizeof (prev_insn),
  397.                info);
  398.         }
  399.  
  400.           /* If there was a problem reading memory, then assume
  401.          the previous instruction was not sethi.  */
  402.           if (errcode == 0)
  403.         {
  404.           /* Is it sethi to the same register?  */
  405.           if ((prev_insn.code & 0xc1c00000) == 0x01000000
  406.               && prev_insn.rd == insn.rs1)
  407.             {
  408.               (*info->fprintf_func) (stream, "\t! ");
  409.               info->target = 
  410.             (0xFFFFFFFF & (int) prev_insn.imm22 << 10)
  411.             | SEX (insn.imm13, 13);
  412.               (*info->print_address_func) (info->target, info);
  413.               info->insn_type = dis_dref;
  414.               info->data_size = 4;  /* FIXME!!! */
  415.             }
  416.         }
  417.         }
  418.  
  419.       if (opcode->flags & (F_UNBR|F_CONDBR|F_JSR))
  420.         {
  421.         /* FIXME -- check is_annulled flag */
  422.           if (opcode->flags & F_UNBR)
  423.         info->insn_type = dis_branch;
  424.           if (opcode->flags & F_CONDBR)
  425.         info->insn_type = dis_condbranch;
  426.           if (opcode->flags & F_JSR)
  427.         info->insn_type = dis_jsr;
  428.           if (opcode->flags & F_DELAYED)
  429.         info->branch_delay_insns = 1;
  430.         }
  431.  
  432.       return sizeof (insn);
  433.     }
  434.     }
  435.  
  436.   info->insn_type = dis_noninsn;    /* Mark as non-valid instruction */
  437.   (*info->fprintf_func) (stream, "%#8x", insn.code);
  438.   return sizeof (insn);
  439. }
  440.  
  441. /* Compare opcodes A and B.  */
  442.  
  443. static int
  444. compare_opcodes (a, b)
  445.      char *a, *b;
  446. {
  447.   struct sparc_opcode *op0 = (struct sparc_opcode *) a;
  448.   struct sparc_opcode *op1 = (struct sparc_opcode *) b;
  449.   unsigned long int match0 = op0->match, match1 = op1->match;
  450.   unsigned long int lose0 = op0->lose, lose1 = op1->lose;
  451.   register unsigned int i;
  452.  
  453.   /* If a bit is set in both match and lose, there is something
  454.      wrong with the opcode table.  */
  455.   if (match0 & lose0)
  456.     {
  457.       fprintf (stderr, "Internal error:  bad sparc-opcode.h: \"%s\", %#.8lx, %#.8lx\n",
  458.            op0->name, match0, lose0);
  459.       op0->lose &= ~op0->match;
  460.       lose0 = op0->lose;
  461.     }
  462.  
  463.   if (match1 & lose1)
  464.     {
  465.       fprintf (stderr, "Internal error: bad sparc-opcode.h: \"%s\", %#.8lx, %#.8lx\n",
  466.            op1->name, match1, lose1);
  467.       op1->lose &= ~op1->match;
  468.       lose1 = op1->lose;
  469.     }
  470.  
  471.   /* Because the bits that are variable in one opcode are constant in
  472.      another, it is important to order the opcodes in the right order.  */
  473.   for (i = 0; i < 32; ++i)
  474.     {
  475.       unsigned long int x = 1 << i;
  476.       int x0 = (match0 & x) != 0;
  477.       int x1 = (match1 & x) != 0;
  478.  
  479.       if (x0 != x1)
  480.     return x1 - x0;
  481.     }
  482.  
  483.   for (i = 0; i < 32; ++i)
  484.     {
  485.       unsigned long int x = 1 << i;
  486.       int x0 = (lose0 & x) != 0;
  487.       int x1 = (lose1 & x) != 0;
  488.  
  489.       if (x0 != x1)
  490.     return x1 - x0;
  491.     }
  492.  
  493.   /* They are functionally equal.  So as long as the opcode table is
  494.      valid, we can put whichever one first we want, on aesthetic grounds.  */
  495.  
  496.   /* Our first aesthetic ground is that aliases defer to real insns.  */
  497.   {
  498.     int alias_diff = (op0->flags & F_ALIAS) - (op1->flags & F_ALIAS);
  499.     if (alias_diff != 0)
  500.       /* Put the one that isn't an alias first.  */
  501.       return alias_diff;
  502.   }
  503.  
  504.   /* Except for aliases, two "identical" instructions had
  505.      better have the same opcode.  This is a sanity check on the table.  */
  506.   i = strcmp (op0->name, op1->name);
  507.   if (i)
  508.       if (op0->flags & F_ALIAS) /* If they're both aliases, be arbitrary. */
  509.       return i;
  510.       else
  511.       fprintf (stderr,
  512.            "Internal error: bad sparc-opcode.h: \"%s\" == \"%s\"\n",
  513.            op0->name, op1->name);
  514.  
  515.   /* Fewer arguments are preferred.  */
  516.   {
  517.     int length_diff = strlen (op0->args) - strlen (op1->args);
  518.     if (length_diff != 0)
  519.       /* Put the one with fewer arguments first.  */
  520.       return length_diff;
  521.   }
  522.  
  523.   /* Put 1+i before i+1.  */
  524.   {
  525.     char *p0 = (char *) strchr(op0->args, '+');
  526.     char *p1 = (char *) strchr(op1->args, '+');
  527.  
  528.     if (p0 && p1)
  529.       {
  530.     /* There is a plus in both operands.  Note that a plus
  531.        sign cannot be the first character in args,
  532.        so the following [-1]'s are valid.  */
  533.     if (p0[-1] == 'i' && p1[1] == 'i')
  534.       /* op0 is i+1 and op1 is 1+i, so op1 goes first.  */
  535.       return 1;
  536.     if (p0[1] == 'i' && p1[-1] == 'i')
  537.       /* op0 is 1+i and op1 is i+1, so op0 goes first.  */
  538.       return -1;
  539.       }
  540.   }
  541.  
  542.   /* They are, as far as we can tell, identical.
  543.      Since qsort may have rearranged the table partially, there is
  544.      no way to tell which one was first in the opcode table as
  545.      written, so just say there are equal.  */
  546.   return 0;
  547. }
  548.